package lv

import (
	"xr-game-server/core/constants/common"
	"xr-game-server/dao/roledao"
	"xr-game-server/module/protocol"
)

type ExpItemHandler struct {
	protocol.ItemHandler
}

func initItemHandler() {
	protocol.RegItemHandler(protocol.Exp, &ExpItemHandler{})
}

func (receiver *ExpItemHandler) AddItem(roleId uint64, item []*protocol.ItemModel, canPush bool) {
	for _, model := range item {
		model.TypeId = common.Add
		roleData := roledao.GetRoleByRole(roleId)
		model.Old = roleData.Exp
		//经验不检查失败值,否则触发邮件
		add(roleId, model.Val, canPush)
		model.Current = roleData.Exp
		return
	}
}

func (receiver *ExpItemHandler) CanConsume(roleId uint64, item *protocol.ItemModel) bool {
	return roledao.GetRoleByRole(roleId).CanReduceExp()
}

func (receiver *ExpItemHandler) Consume(roleId uint64, items []*protocol.ItemModel, canPush bool) bool {
	for _, item := range items {
		item.TypeId = common.Subtract
		roleData := roledao.GetRoleByRole(roleId)
		item.Old = roleData.Exp
		//经验不检查失败值,否则触发邮件
		reduce(roleId, item.Val, canPush)
		item.Current = roleData.Exp
		return true
	}
	return true
}
